package br.ufc.great.dataguide;

import java.io.Serializable;
import java.util.Vector;

/**
 * 
 * @author Leonardo Oliveira Moreira
 *
 * Classe que representa uma lista de bloqueios de um elemento XML
 */
public class LockList implements Serializable {

	private Vector<LockItem> itens;
	
	public LockList() {
		itens = new Vector<LockItem>();
	}
	
	public boolean add(String transactionId, String operationId, int lock) {
		for (int i = 0; itens != null && i < itens.size(); i++) {
			LockItem item = itens.elementAt(i);
			if (item.getTransactionId().equalsIgnoreCase(transactionId) && item.getOperationId().equalsIgnoreCase(operationId) && item.getLockType() == lock)
				return false;
		}
		itens.add(new LockItem(transactionId, operationId, lock));
		return true;
	}

	public boolean remove(String transactionId, int lock) {
		for (int i = 0; itens != null && i < itens.size(); i++) {
			LockItem item = itens.elementAt(i);
			if (item.getTransactionId().equalsIgnoreCase(transactionId) && item.getLockType() == lock) {
				itens.removeElementAt(i);
				return true;
			}
		}
		return false;
	}
	
	public void remove(String transactionId) {
		for (int i = 0; itens != null && i < itens.size(); i++) {
			LockItem item = itens.elementAt(i);
			if (item.getTransactionId().equalsIgnoreCase(transactionId)) {
				itens.removeElementAt(i);
				i--;
			}
		}
	}
	
	public void remove(String transactionId, String operationId) {
		for (int i = 0; itens != null && i < itens.size(); i++) {
			LockItem item = itens.elementAt(i);
			if (item.getTransactionId().equalsIgnoreCase(transactionId) && item.getOperationId().equalsIgnoreCase(operationId)) {
				itens.removeElementAt(i);
				i--;
			}
		}
	}
	
	public LockItem get(int index) {
		return itens.elementAt(index);
	}
	
	public int size() {
		return itens.size();
	}
	
}
